#ifndef __GND_MODULE_APPMANAGER_H__
#define __GND_MODULE_APPMANAGER_H__

#include <gtkmm/treeview.h>
#include <gtkmm/treemodelcolumn.h>
#include <gtkmm/treestore.h>
#include <gtkmm/button.h>
#include <map>

#include "Module.h"
#include "Global.h"

namespace Gnodroid {

    /** 应用管理模块
     */
    class ModuleAppmanager :
        public Module
    {
    public:
        ModuleAppmanager();

        void start();
        void stop();

    protected:
    private:
        /** Treeview中Model的各列
         */
        class ModelColumns : public Gtk::TreeModelColumnRecord
        {
        public:
            ModelColumns()
            {
                add(type);
                add(icon);
                add(label);
                add(package_name);
                add(version);
                add(appType);
                add(hasSetIcon);
            }

            enum AppType {
                APPTYPE_USER = 0,
                APPTYPE_SYS,
                APPTYPE_NONE,
            };

            Gtk::TreeModelColumn<Glib::RefPtr<Gdk::Pixbuf>> icon;
            Gtk::TreeModelColumn<std::string> type;
            Gtk::TreeModelColumn<std::string> label;
            Gtk::TreeModelColumn<std::string> package_name;
            Gtk::TreeModelColumn<std::string> version;
            Gtk::TreeModelColumn<AppType> appType;
            Gtk::TreeModelColumn<bool> hasSetIcon;
        protected:
        private:
        };

        /** 应用信息缓存
         */
        class AppCache
        {
        public:
            bool isEmpty {true};
            Glib::RefPtr<Gdk::Pixbuf> icon;     //应用图标
        protected:
        private:
        };

        Gtk::TreeView* m_pTvApplist {nullptr};
        Gtk::Button* m_pBtnAppInst {nullptr};
        Gtk::Button* m_pBtnAppRefresh {nullptr};
        Gtk::Button* m_pBtnAppUninst {nullptr};
        Gtk::Button* m_pBtnAppSave {nullptr};

        ModelColumns m_modelColumns;
        Glib::RefPtr<Gtk::TreeStore> m_refTreeStore {nullptr};          //Model

        std::shared_ptr<std::list<AndroidApp>> m_shrUserApps {nullptr};     //用户应用列表
        std::shared_ptr<std::list<AndroidApp>> m_shrSysApps {nullptr};      //系统应用列表
        std::map<std::string, AppCache> m_mapIconCache;                     //应用信息缓存
        std::string m_strDeviceId;

        bool m_bIsRefreshing {false};

        void __refreshAppList();
        void __updateAppListStore();
        void __getAppIcons();
        void __updateAppIcons();

        void __clearAppList();

    };

}


#endif
